//
//  MineViewController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/27.
//

#import "MineViewController.h"
#import "MineHeaderView.h"
#import "MineModel.h"
#import "MineTableViewCell.h"
#import "EditViewController.h"
#import "QRCodelViewController.h"
#import "BlackListController.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MineHeaderView *headerView;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *modelArr;

@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}
- (void)initDidLoad {
    self.hbd_barHidden = YES;
}
- (void)initViews {
    [self.view addSubview:self.tableview];
    self.tableview.tableHeaderView = self.headerView;
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)initRequestData {
    self.headerView.model = kUserAccount.user;
    
    MineModel *model = [[MineModel alloc] init];
    model.imageStr = @"mine_feed";
    model.title = @"问题反馈";
    model.type = MineType_feed;
    [self.modelArr addObject:model];
    
    model = [[MineModel alloc] init];
    model.imageStr = @"mine_follow";
    model.title = @"黑名单";
    model.type = MineType_follow;
    [self.modelArr addObject:model];
    
    model = [[MineModel alloc] init];
    model.imageStr = @"mine_agreement";
    model.title = @"用户协议";
    model.type = MineType_userAggrement;
    [self.modelArr addObject:model];
    
    model = [[MineModel alloc] init];
    model.imageStr = @"mine_privacy";
    model.title = @"隐私政策";
    model.type = MineType_privacy;
    [self.modelArr addObject:model];
    
    model = [[MineModel alloc] init];
    model.imageStr = @"mine_delete";
    model.title = @"删除账号";
    model.type = MineType_delete;
    [self.modelArr addObject:model];
    
    model = [[MineModel alloc] init];
    model.imageStr = @"mine_logout";
    model.title = @"退出登录";
    model.type = MineType_logout;
    [self.modelArr addObject:model];
    
    [self.tableview reloadData];
}

- (void)requestData {
    [NET getUserInfo:kUserAccount.user.id success:^(id  _Nonnull responseObject) {
        UserModel *model = [UserModel mj_objectWithKeyValues:responseObject];
        kUserAccount.user = model;
        self.headerView.model = model;
    } failure:^(NSError * _Nonnull error) {
    }];
}

#pragma mark - 懒加载
- (MineHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidthForScreen, 110)];
        @weakify(self)
        _headerView.editUserBlock = ^{
         @strongify(self)
            EditViewController *vc = [[EditViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
        _headerView.showQrcodeBlock = ^{
            @strongify(self)
            QRCodelViewController *vc = [[QRCodelViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
    }
    return _headerView;
}
- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.delegate = self;
        _tableview.dataSource = self;
    }
    return _tableview;
}

- (NSMutableArray *)modelArr {
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MineModel *model = self.modelArr[indexPath.row];
    switch (model.type) {
        case MineType_feed:{
            [UIViewController goWebController:@"问题反馈" url:@"https://service.456465465465465.top/index/index/home?visiter_id=&visiter_name=&avatar=&business_id=1&groupid=1&special=1"];
        }break;
        case MineType_follow:{
            BlackListController *vc = [[BlackListController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case MineType_userAggrement:{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://static.456465465465465.top/services/UserAgreement.html"] options:@{UIApplicationOpenURLOptionsSourceApplicationKey:@(YES)} completionHandler:^(BOOL success) {
            }];
        }break;
        case MineType_privacy:{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://static.456465465465465.top/services/PrivacyPolicy.html"] options:@{UIApplicationOpenURLOptionsSourceApplicationKey:@(YES)} completionHandler:^(BOOL success) {
            }];
        }break;
        case MineType_delete:{
            [UIAlertController showAlertInViewController:self withTitle:@"确认删除本账号?" message:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:@[] tapBlock:^(UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
                if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) {
                    kShowText(@"账号删除申请已提交,官方将在24小时内进行账号清理,谢谢");
                }
            }];
        }break;
        case MineType_logout:{
            [UIAlertController showAlertInViewController:self withTitle:@"确认退出本账号?" message:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"退出" otherButtonTitles:@[] tapBlock:^(UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
                if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) {
                    [kUserAccount logout];
                }
            }];
        }break;
        default:
            break;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCell"];
    if (!cell) {
        cell = [[MineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MineTableViewCell"];
    }
    cell.model = self.modelArr[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

@end
